#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->front = NULL;
	q->rear = NULL;
	q->size = 0;
}

QlistNode* BuyQueueNode(QueuedateType x)
{
	QlistNode* cur = (QlistNode*)malloc(sizeof(QlistNode));
	cur->data = x;
	cur->next = NULL;
	return cur;
}

void QueuePush(Queue* q, QueuedateType data)
{
	QlistNode* Newnode = BuyQueueNode(data);
	assert(q);
	if (q->rear == NULL)
	{
		assert(q->front == NULL);
		q->front = q->rear = Newnode;

	}
	else
	{
		q->rear->next = Newnode;
		q->rear = Newnode;
	}
	q->size++;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	QlistNode* cur = q->front;
	while (cur)
	{
		QlistNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->size = 0;
	q->front = q->rear = NULL;
}

bool EmptyQueue(Queue* q)
{
	assert(q);
	return q->size == 0;
}


void QueuePop(Queue* q)
{
	assert(q);
	assert(!EmptyQueue(q));
	QlistNode* tmp = q->front->next;
	free(q->front);
	q->front = tmp;
	q->size--;

}

QueuedateType QueueFront(Queue* q)
{
	assert(q);
	assert(EmptyQueue);
	return q->front->data;
}

QueuedateType QueueBack(Queue* q)
{
	assert(q);
	assert(EmptyQueue);
	return q->rear->data;
}

bool QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}